package com.loren.demo07.sockjs.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker
public class SockJsConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/sockjs/websocket").setAllowedOrigins("*").withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        // 点对点时的订阅前缀，默认：/user
//        registry.setUserDestinationPrefix("/client");

        // 启用三条通道，通道的前缀分别为: /group, /user, /message
        // 注意：当不修改点对点的订阅前缀时，可以不写明，写出来只是为了明显的声明而已。否则必须声明
        registry.enableSimpleBroker("/group", "/user", "/message");
    }
}
